;;; ~/.doom.d/Hesperus-keybindings.el -*- lexical-binding: t; -*-

(defvar Hesperus-doom-config "~/.doom.d/config.el")
(defun Hesperus-open-doom ()
  "Open config.el in Doom Emacs path."
  (interactive)
  (find-file Hesperus-doom-config)
  (current-buffer))
(map! "C-c d" #'Hesperus-open-doom
      "s-d" #'Hesperus-open-doom)

(defun Hesperus-open-newline-inline ()
  "Open a newline even if the cursor is Not at end."
  (interactive)
  (end-of-visual-line)
  (newline-and-indent))
(dolist (HA-newline-inline-hook
         (list
          'markdown-mode-hook
          'prog-mode-hook))
  (add-hook HA-newline-inline-hook
            (lambda ()
              (local-set-key (kbd "C-<return>")
                             #'Hesperus-open-newline-inline))))

;; @mayingjun via emacs-cn
(defun Hesperus-delete-empty-line (beg end)
  "Delete empty lines if selected, otherwise the whole buffer."
  (interactive "r")
  (let ((empty-line "^[\t  ]*$"))
    (save-excursion
      (if (region-active-p)
          (flush-lines empty-line beg end)
        (flush-lines empty-line (point-min) (point-max))))))

(map!
 ;; mask unexpected keybindings as default
 "C-b" 'backward-char
 "C-f" 'forward-char
 "M-b" 'backward-word
 "M-c" 'capitalize-word
 "M-f" 'forward-word
 "M-v" 'scroll-down-command
 "M-w" 'copy-region-as-kill

  ;; switch diff Modes
 (:prefix "C-c m"
   :desc "Σlisp" "e" 'emacs-lisp-mode
   :desc "LaTeX" "l" 'latex-mode
   :desc "M↓" "m" 'markdown-mode
   :desc "Org" "o" 'org-mode
   :desc "Python" "p" 'python-mode
   :desc "R" "r" 'R-mode
   :desc "TeX" "t" 'tex-mode
   :desc "teXt" "x" 'text-mode
   :desc "yaml" "y" 'yaml-mode)
 (:prefix "s-m"
   :desc "Σlisp" "e" 'emacs-lisp-mode
   :desc "LaTeX" "l" 'latex-mode
   :desc "M↓" "m" 'markdown-mode
   :desc "Org" "o" 'org-mode
   :desc "Python" "p" 'python-mode
   :desc "R" "r" 'R-mode
   :desc "TeX" "t" 'tex-mode
   :desc "teXt" "x" 'text-mode
   :desc "yaml" "y" 'yaml-mode)

 ;; use helm-mini to switch Buffers
 "C-c b" 'helm-mini
 "s-b" 'helm-mini

 ;; Describe-char and codepoint
 "C-c D" 'describe-char
 "s-D" 'describe-char

 ;; Find file in project
 "C-c f" 'helm-projectile-find-file
 "s-f" 'helm-projectile-find-file

 ;; popup maGit status
 "s-g" 'magit-status

 ;; Jump with avy-goto-char
 "C-c j" 'avy-goto-char
 "s-j" 'avy-goto-char

 ;; company select
 (:after company
   (:map company-active-map
     "C-n" #'company-select-next
     "C-p" #'company-select-previous))

 ;; search and replace with Swiper and Replace-regexp
 "C-S-r" 'query-replace-regexp
 "C-s" 'swiper
 "C-S-s" 'isearch-forward-regexp

 ;; similar with the function of "=" in vim
 "C-=" 'indent-region

 ;; remap M-^ to M-6 (Not type shift)
 "M-6" 'delete-indentation

 "M-;" #'evil-indent

 )

(provide 'Hesperus-keybindings)
